package agrep4me;

import java.io.IOException;
import java.io.InputStreamReader;

class Fichier {
	private static final int BUF_SIZE = 105;
	private static final int ERROR = 5;
	private InputStreamReader isr;
	private char[] buf = new char[BUF_SIZE];
	private boolean firstBuffer = true;
	private int count;

	Fichier(InputStreamReader isr) {
		this.isr = isr;
	}

	String nextString() {
		String r = null;

		try {
			if (firstBuffer) {
				count = isr.read(buf, 0, BUF_SIZE - ERROR);
				firstBuffer = false;
				r = new String(buf, 0, count);
			} else {
				for (int i = 0; i < ERROR; i++) {
					buf[i] = buf[count - ERROR + i];
				}
				count = isr.read(buf, ERROR, BUF_SIZE - ERROR);
				r = new String(buf, 0, count + ERROR);
			}

			if (count <= 0) {
				return null;
			}

			// System.out.println("<" + r + ">");
		} catch (IOException e) {
			System.err.println(e.getMessage());
			System.exit(2);
		}
		return r;
	}

	void close() {
		try {
			isr.close();
		} catch (IOException e) {
			System.err.println(e.getMessage());
			System.exit(2);
		}
	}

}
